<?php


/**
 * 用户管理Action
 * Enter description here ...
 * @author Admin
 *
 */
class UserAction extends CommonAction{
	
	
	/**
	 * 登陆页面
	 * Enter description here ...
	 */
	public function signin()	{
		$this->display();
	}

	
	/**
	 * 注册页面
	 * Enter description here ...
	 */
	public function register(){
		$this->display();
	}
	
	

	/**
	 * 保密协议
	 * 注册邮箱认证通过后保存用户信息
	 * Enter description here ...
	 */
	public function privacy(){
		$this->display();
	}

	/**
	 * 使用条款
	 * 注册邮箱认证通过后保存用户信息
	 * Enter description here ...
	 */
	public function agreement(){
		$this->display();
	}


	
	/**
	 * 登录注册验证码
	 * Enter description here ...
	 */
	public function verify(){
		import("@.ORG.Image"); 
		Image::buildImageVerify(4,1,"png",60,30);
	}
	
	
	
	

	/**
	 * 添加用户发送邮件
	 * Enter description here ...
	 */
	public function addmember(){
		$verification = D('Verification');  //认证管理
		
		$smtp = A('Smtp');   //邮箱SMTP管理
		
		$data = Init_GP(array('mail','code'));  //邮箱和验证码
		
		if ($this->_checkVerify($data['code'])){
			if ($this->_checkEmail($data['mail'])){
				$verification->delCode($data['mail'],'verification');
				$code = $verification->setCode($data['mail'],'verification');
				
				$url = HOST.__URL__.'/actmember/code/'.$code;
				
				$header = '邮箱验证确认信';
				
				import("@.ORG.Mail"); //导入邮件类
				$body = Mail::template('verification'); //选择模板 --注册认证
				$body = str_replace('[mail]',$data['mail'], $body);
				$body = str_replace('[url]',$url, $body);

				$info = $smtp->send($data['mail'],$header,$body);
				
				if ($info === true){
					echo 0;
				}elseif ($info === false){
					echo  '发送失败';
				}else {
					echo $info;
				}
			}else {
				echo '邮箱错误';
			}
		}else {
			echo '验证码错误';
		}
	}

	
	
	/**
	 * 再次发送邮件
	 * Enter description here ...
	 */
	public function resendmail(){
		$this->addmember();
	}
	
	

	/**
	 * 注册前验证邮箱是否已使用
	 * Enter description here ...
	 */
	public function peraddcheck(){
		$data = Init_GP(array('userEmail'));
		$info = $this->_checkEmail($data['userEmail']);
		if ($info) {
			echo json_encode(1);
		}else {
			echo json_encode(0);
		}
	}
	
	

	/**
	 * 注册前邮箱认证通过后    验证用户名是否可用
	 * Enter description here ...
	 */
	public function peraddnicknamecheck(){
		$data = Init_GP(array('userNickname'));
		$info = $this->_checkName($data['userNickname']);
		if ($info) {
			echo json_encode(1);
		}else {
			echo json_encode(0);
		}
	}

	
	
	
	
	/**
	 * 邮箱验证 激活信息
	 * Enter description here ...
	 */
	public function actmember(){
		$data = Init_GP(array('code'));
		$verification = D('Verification');   //认证管理
		
		$head = M('Head');  //用户头像
		 
		$map = array(
			'code'=>array('eq',$data['code']),
			'type'=>array('eq','verification')
		);

		$verificationdata = $verification->where($map)->find();
		
		$headdata=$head->field('filename')->order('RAND()')->find();  

		$this->assign("headdata",$headdata);
		$this->assign('verificationdata',$verificationdata);
		
		$this->display();
	}



	
	/**
	 * 提交注册
	 * 注册邮箱认证后用户确认注册
	 * Enter description here ...
	 */
	public function regsetp2(){
		$json = null;

		$config = C('SYSCONFIG');
		
		$agreement = Init_GP(array('paibanAgreement'));
		$secretsn = Init_GP(array('secretsn'));
		
		$verification = D('Verification');
		$map = array(
			'code'=>array('eq',$secretsn['secretsn']),
			'type'=>array('eq','verification')
		);
		
		$verificationdata = $verification->where($map)->find();
		
		if (!empty($agreement['paibanAgreement']) && $agreement['paibanAgreement'] == 'on'){
			$user = D('User');
			//$data = Init_GP(array('userEmail','userNickname','userPassword','photo','realname','phoneno','sex','birthday'));

			//...赠送一定数量的虚拟币
			$data = Init_GP(array('userEmail','userNickname','userPassword','photo','realname','phoneno','sex','birthday'));
			
			$data['pointb'] = $config['gift_pointb'];  //注册赠送虚拟币
			
			$userNmae = $data['userNickname'];  //用户名称
			
			if ($verificationdata['mail'] == $data['userEmail']){
				
				$password = $data['userPassword']; //做为发送邮件的密码
				$mail = $data['userEmail']; //做为发送邮件的邮箱
				$data = $user->create($data,1);

				if(empty($data) || $data === false) {
					$json = 1; //返回1不成功
				}else {
					$id = $user->add($data);
					
					if($id) { //保存成功
						

						
						$transaction1 = D('Transaction');  //交易日志---新用户注册赠送虚拟币
						$transactiondata1 = array(
							'uid'=>$id,  //用户ID
							'name' => $userNmae,  //用户名称
							'payment'=>'注册赠送',  //支付人
							'addtime'=>time(), 
							'remark'=>"新注册赠送{$config['gift_pointb']}{$config['pointb_name']}",
						
							'money'=>$config['gift_pointb'],
							'type'=>"pointb",  //虚拟币
							'source'=>'10',
							'robot'=>'0'
						);
						$tt = $transaction1->add($transactiondata1);  
						
						
						
						
						//成功提示
						$smtp = A('Smtp');
						$verification->delCode($data['mail'],'verification');
						cookie('mb_name',$data['mail']);

						$header = '感谢您注册';
						import("@.ORG.Mail"); //导入邮件类
						$body = Mail::template('success'); //选择模板
						$body = str_replace('[mail]',$mail, $body);
						$body = str_replace('[password]',$password,$body);

						$smtp->send($mail,$header,$body);   //注册成功发送邮件

						$json = 0; //返回0成功

						
						
						
						
						//TODO:.....通过好友推荐连接登录首页IndexAction中invite中存放cookie  存放的是推荐人ID
						
						$invite = cookie('invite');
						$invitedata = $user->find($invite);   //查找推荐人
						if (!empty($invitedata)){  //存在系统用户
						
							//....setInc() 方法将数字字段值增加
							$info = $user->setInc('pointb',"id = {$invite}",$config['invite']);    //将用户的虚拟币增加指定推荐好友赠送值
							
							if ($info){
								
								$transaction = D('Transaction');  //交易日志
								$transactiondata = array(
									'uid'=>$invitedata['id'],  //用户ID
									'name' => $invitedata['name'],  //用户名称
									'payment'=>'邀请奖励',  //支付人
									'addtime'=>time(), 
									'remark'=>"邀请用户奖励{$config['invite']}{$config['pointb_name']}",
								
									'money'=>$config['invite'],
									'type'=>"pointb",  //虚拟币
									'source'=>'2',
									'robot'=>'0'
								
								);
								$transaction->add($transactiondata);    //保存交易日志
								
							}
						}
						
						
						
						
						
						
					}else {
						//失败提示
						$json = 1; //返回1不成功
					}
				}
			}else {
				$json = 1; //返回1不成功
			}
		}else {
			$json = 1; //返回1不成功
		}

		echo json_encode($json);
	}

	
	
	
	
	
	/**
	 * 验证成功后跳转地址
	 * Enter description here ...
	 */
	public function jumper(){
		$this->display();
	}
	
	
	
	
	
	

	/**
	 * 登陆认证
	 * Enter description here ...
	 */
	public function ajaxlogin(){
		$json = null;
		$err = 0;
		$data = Init_GP(array('username','password','logincode'));
		$loginwrongtime = GetNum(cookie('loginwrongtime'));
		$info = preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$data['username']);
		if ($info === 1){
			if ($loginwrongtime <= 2){
				$state = $this->_checkLogin($data['username'],$data['password']);
				if ($state){
					$json = 0;
				}else {
					$json = 1;
					$err = 1;
				}
			}else {
				$info = $this->_checkVerify($data['logincode']);
				if ($info){
					$state = $this->_checkLogin($data['username'],$data['password']);
					if ($state){
						$json = 0;
					}else {
						$json = 1;
						$err = 1;
					}
				}else {
					$json = 2;
					$err = 1;
				}
			}
		}else {
			$err = 1;
		}
		
		$loginwrongtime = $loginwrongtime+$err;
		
		cookie('loginwrongtime',$loginwrongtime);
		
		echo json_encode($json);
	}
	
	
	
	
	
	/**
	 * 检测登陆
	 * Enter description here ...
	 */
	public function inimember(){
		$data = $_SESSION['user'];
		$json = null;
		if (!empty($data)){
			$json['nickname'] = $data['name'];
		}else {
			$this->_logclear();
		}

		$letter = M('Letter');  //个人消息[不是全局消息]
		$map['receptionname'] = array('eq',$data['name']);
		$map['state'] = array('eq',0);
		$json['newNum'] = $letter->where($map)->count();
		
		if (!empty($json)){
			echo json_encode($json);
		}else {
			send_http_status(400);
		}
	}
	
	
	
	
	
	/**
	 * 退出
	 * Enter description here ...
	 */
	public function logout(){
		$this->_logclear();
		echo json_encode(1);
	}

	
	
	/**
	 * 清空session和cookie
	 * Enter description here ...
	 */
	public function _logclear(){
		unset($_SESSION['user']);
		cookie('islogin',null,-99999999);
		cookie('mb_id',null,-99999999);
		cookie('isgetPointB',null,-99999999);
	}
	
	
	
	
	
	
	/**
	 * 找回密码
	 * Enter description here ...
	 */
	public function getpwd(){
		$data = Init_GP(array('mailbox','forgetcode'));
		$json = null;
		if ($this->_checkVerify($data['forgetcode'])){
			
			if ($this->_isMember($data['mailbox'])){
				
				$verification = D('Verification');   //认证Model
				
				$smtp = A('Smtp');  //邮箱SMTP配置
				$count = $verification->getCount($data['mailbox'],'getpwd');  //获取申请找回密码邮箱次数
				
				if ($count <= 2){
					$verification->delCode($data['mailbox'],'getpwd');
					$code = $verification->setCode($data['mailbox'],'getpwd',$count+1);
						
					$url = HOST.__URL__.'/setpwd/code/'.$code;
					$header = '找回密码操作确认';
					
					import("@.ORG.Mail"); //导入邮件类
					$body = Mail::template('getpwd'); //选择模板
					$body = str_replace('[url]',$url, $body);
					
					$info = $smtp->send($data['mailbox'],$header,$body);   //发送邮件
					
					$json = 0;
				}else {
					$json = 3;
				}
			}else {
				$json = '邮箱不存在！';
			}
			
		}else {
			$json = '验证码错误！';
		}
		echo $json;
	}

	
	
	
	
	
	
	/**
	 * 重设密码结果
	 * Enter description here ...
	 */
	public function setpwd(){
		$data = Init_GP(array('code'));
		
		$verification = D('Verification');  //认证管理
		$user = D('User');  //用户
		$map = array(
			'code'=>array('eq',$data['code']),
			'type'=>array('eq','getpwd')
		);

		$verificationdata = $verification->where($map)->find();
		
		if (!empty($verificationdata)){
			$password = $user->setpwd($verificationdata['mail']);
			$verification->delCode($verificationdata['mail'],'getpwd');
			$this->assign('password',$password);
		}

		
		$this->assign('verificationdata',$verificationdata);
		$this->display();
	}

	
	
	
	
	
	
	
	
	
	/**
	 * 检测用户状态
	 * Enter description here ...
	 */
	public function ajaxcheckuser(){
		$info= $this->_updatestate();
		if ($info) {
			echo 0; //成功返回0
		}else {
			echo 2; //失败返回2
		}
	}
	
	
	/**
	 * 更新状态
	 * Enter description here ...
	 */
	public function _updatestate(){
		$userdata = $this->_islogin();  //是否登录
		if (!empty($userdata)){
			$_SESSION['user'] = $userdata;

			
			
			//TODO:..................注册用户打卡争取虚拟币
			/*   */
			$punch = D('Punch');

			if ($punch->isPunch($userdata['id'])){
				cookie('isgetPointB',1); //已经打卡
			}else {
				cookie('isgetPointB',0); //为打卡
			}
			
			
			
			
			return true;
		}else {
			$this->_logclear();
			
			return false;
		}
		
	}
	
	
	
	
	
	/**
	 * 获取统计信息
	 * Enter description here ...
	 */
	public function getcount(){
		$json = null;
		$data = $this->_islogin();   //是否登录

		if (empty($data)){
			$json['islogin']= 0;
		}else {
			$json['islogin']= 1;
			$json['a_point']= $data['beat'];  //实体币
			$json['b_point']= $data['pointb']; //虚拟币
			//$json['c_point']= $data['pointc']; //积分
		}
		echo json_encode($json);
	}


	
	
	
	
	
	
	/**
	 * 个人中心
	 * 注册用户查看外部首页
	 * 其他用户查看个人信息
	 * Enter description here ...
	 */
	//TODO:
	public function center(){
		$data = Init_GP(array('id'));   //用户ID
		$id = GetNum($data['id']);
		
		if(!empty($id)){
			
			$user = D('User');    //个人信息
			$userdata = $user->find($id);

			//参与过的竞拍
			$participate = D('participate');
			$participatedata = $participate->getAuctionData($id,10);
				
			$this->assign('participatedata',$participatedata);
			
			$this->assign('userdata',$userdata);
		}
		
		$this->display();
	}


	
	
	
	/**
	 * 
	 * 是否登陆 返回数据库中数据
	 * MemberAction
	 * Enter description here ...
	 */
	public function _islogin(){
		$data = $_SESSION['user'];
		if (empty($data)) {
			return false;
		}else {
			$user = D('User');
			$userdata = $user->find($data['id']);
			if (empty($userdata)){
				return false;
			}else {
				return $userdata;
			}
		}
	}
	
	
	
	/**
	 * 是否是会员邮箱
	 * Enter description here ...
	 * @param unknown_type $mail
	 */
	public function _isMember($mail){
		$user = D('User');
		$map['mail'] = array('eq',$mail);
		$userdata = $user->where($map)->find();
		if (empty($userdata)){
			return false;
		}else {
			return true;
		}
	}
	
	
	
	/**
	 * 验证登陆
	 * Enter description here ...
	 * @param unknown_type $mail
	 * @param unknown_type $password
	 */
	public function _checkLogin($mail,$password){
		$user = D('User');
		$map = array(   //邮箱密码
			'mail' =>array('eq',$mail),
			'password'=>array('eq',md5($password)),
			'lockstate'=>0 
		);

		$data = $user->where($map)->find();
		if (empty($data) || $data===false){
			return false;
		}else {
			
			
			//登录成功记录登录日志
			$loginlog = D('Loginlog');
			$loginlogdata=array(
				'uid'=>$data['id'],
				'name'=>$data['name'],
			);
			$loginlogdata = $loginlog->create($loginlogdata,1);
			
			$loginlog->add($loginlogdata);
			
			//....登录信息写入cookie
			$_SESSION['user'] = $data;
			cookie('islogin','1',9999999);
			cookie('mb_name',$data['mail']);
			cookie('mb_id',$data['id'],9999999);
			
			
			return true;
		}
	}
	
	
	
	

	/**
	 * 验证邮箱
	 * Enter description here ...
	 * @param unknown_type $email
	 */
	protected function _checkEmail($email){
		$user = D('User');
		if(empty($email)) {
			return false;
		}else {
			$info = preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email);
			if ($info === 1){
				return !$this->_isMember($email);
			}else {
				return true;
			}
		}
	}

	
	
	/**
	 * 验证昵称
	 * Enter description here ...
	 * @param unknown_type $name
	 */
	protected function _checkName($name){
		$user = D('User');
		if(empty($name)) {
			return false;
		}else {
			$map['name'] = array('eq',$name);
			$userdata = $user->where($map)->findAll();
			if (empty($userdata)){
				return true;
			}else {
				return false;
			}
		}
	}

	
	
	/**
	 * 验证验证码
	 * Enter description here ...
	 * @param unknown_type $verify
	 */
	protected function _checkVerify($verify){
		if (empty($verify)){
			return false;
		}else {
			if (md5($verify) != $_SESSION['verify']){
				return false;
			}else {
				return true;
			}
		}
	}
	
	
	
	
	
	/**
	 * 推荐好友
	 * 用户推荐好友
	 * Enter description here ...
	 */
	public function copy_code()	{
		$data = Init_GP(array('id'));    //会员ID
		$id = GetNum($data['id']);
		
		$this->assign('id',$id );
		$this->display();
	}

	
	
	
	/**
	 * 列出头像
	 * 用户修改个性头像
	 * Enter description here ...
	 */
	public function memberlogo(){
		$head = M('Head');
		$headdata=$head->field('filename')->order('RAND()')->limit(36)->findAll();
		$this->assign("headdata",$headdata);
		
		$info = $this->_islogin();
		$this->assign("info",$info);
		
		$this->display();
	}
	
	
	
	
	
}
?>